package com.dayu.web.controller.recovery;

import java.math.BigDecimal;
import java.util.List;

import com.dayu.common.annotation.RepeatSubmit;
import com.dayu.recovery.domain.RecoveryGoods;
import com.dayu.recovery.service.IRecoveryGoodsService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.dayu.common.annotation.Log;
import com.dayu.common.core.controller.BaseController;
import com.dayu.common.core.domain.AjaxResult;
import com.dayu.common.enums.BusinessType;
import com.dayu.recovery.domain.RecoveryModify;
import com.dayu.recovery.service.IRecoveryModifyService;
import com.dayu.common.utils.poi.ExcelUtil;
import com.dayu.common.core.page.TableDataInfo;

/**
 * 调价记录Controller
 *
 * @author HK
 * @date 2021-04-16
 */
@RestController
@RequestMapping("/recovery/modify")
public class RecoveryModifyController extends BaseController
{
    @Autowired
    private IRecoveryModifyService recoveryModifyService;
    @Autowired
    private IRecoveryGoodsService goodsService;
    /**
     * 查询调价记录列表
     */
    @PreAuthorize("@ss.hasPermi('recovery:modify:list')")
    @GetMapping("/list")
    public TableDataInfo list(RecoveryModify recoveryModify)
    {
        startPage();
        List<RecoveryModify> list = recoveryModifyService.selectRecoveryModifyList(recoveryModify);
        return getDataTable(list);
    }

    /**
     * 导出调价记录列表
     */
    @PreAuthorize("@ss.hasPermi('recovery:modify:export')")
    @Log(title = "调价记录", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(RecoveryModify recoveryModify)
    {
        List<RecoveryModify> list = recoveryModifyService.selectRecoveryModifyList(recoveryModify);
        ExcelUtil<RecoveryModify> util = new ExcelUtil<RecoveryModify>(RecoveryModify.class);
        return util.exportExcel(list, "modify");
    }

    /**
     * 获取调价记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('recovery:modify:query')")
    @GetMapping(value = "/{modifyId}")
    public AjaxResult getInfo(@PathVariable("modifyId") Long modifyId)
    {
        return AjaxResult.success(recoveryModifyService.selectRecoveryModifyById(modifyId));
    }

    /**
     * 新增调价记录
     */
    @RepeatSubmit
    @PreAuthorize("@ss.hasPermi('recovery:modify:add')")
    @Log(title = "调价记录", businessType = BusinessType.INSERT)
    @PostMapping
    @Transactional
    public AjaxResult add(@RequestBody RecoveryModify recoveryModify)
    {
        RecoveryGoods goods = goodsService.selectRecoveryGoodsById(recoveryModify.getOrderNum());
        if(goods == null) {
            return AjaxResult.error("没有找到数据，请输入正确的商品编码！");
        }

        if(ObjectUtils.isEmpty(goods.getCostPrice())) {
            goods.setCostPrice(new BigDecimal(0));
        }
        recoveryModify.setPrePrice(goods.getCostPrice());
        recoveryModify.setGoodsName(goods.getGoodsName());
        if(recoveryModify.getStatus() == 1) {
            recoveryModify.setSufPrice(goods.getCostPrice().add(recoveryModify.getModPrice()));
        }else {
            recoveryModify.setSufPrice(goods.getCostPrice().subtract(recoveryModify.getModPrice()));
        }
        goods.setCostPrice(recoveryModify.getSufPrice());
        goods.setUpdateStatus(4);
        int num = goodsService.updateRecoveryGoods(goods);
        if(num > 0) {
            recoveryModify.setGoodsUpdate(1);   //成功
        }else {
            recoveryModify.setGoodsUpdate(2);   //失败
        }
        return toAjax(recoveryModifyService.insertRecoveryModify(recoveryModify));
    }

    /**
     * 修改调价记录
     */
    @PreAuthorize("@ss.hasPermi('recovery:modify:edit')")
    @Log(title = "调价记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody RecoveryModify recoveryModify)
    {
        return toAjax(recoveryModifyService.updateRecoveryModify(recoveryModify));
    }

    /**
     * 删除调价记录
     */
    @PreAuthorize("@ss.hasPermi('recovery:modify:remove')")
    @Log(title = "调价记录", businessType = BusinessType.DELETE)
	@DeleteMapping("/{modifyIds}")
    public AjaxResult remove(@PathVariable Long[] modifyIds)
    {
        return toAjax(recoveryModifyService.deleteRecoveryModifyByIds(modifyIds));
    }
}
